var donneesMembre = new Object();
var donneesTriees = new Object();
var statsRangs = new Array();
var sortData = 'f_nom';
var sorting = 'asc';

statsRangs['ER'] = Array(600, 900, 1200, 1500, 1800, 9999)
statsRangs['ER_Rg'] = Array('bad', 'baverage', 'average', 'good', 'great', 'unicum');

statsRangs['WN7'] = Array(500, 699, 899, 1099, 1349, 1549, 1849, 2049, 9999)
statsRangs['WN7_Rg'] = Array('vbad','bad', 'baverage', 'average', 'good', 'vgood', 'great', 'unicum', 'sunicum');

statsRangs['WN8'] = Array(300, 599, 899, 1249, 1599, 1899, 2349, 2899, 9999)
statsRangs['WN8_Rg'] = Array('vbad','bad', 'baverage', 'average', 'good', 'vgood', 'great', 'unicum', 'sunicum');

var statfilter = function(event) {
	filtrerTableau(event.target.id);
}
var tdOrder = Array('f_nom', 'f_battle', 'f_wr', 'f_survie', 'f_dgt', 'f_exp',
		'f_cap', 'f_def', 'f_kill', 'f_kdratio', 'f_tier', 'f_er', 'f_wn7',
		'f_wn8');

window.onload = function(event) {
	var lstTh = document.getElementsByTagName('TH');
	for (t = 0; t < lstTh.length; t++) {
		lstTh[t].addEventListener('click', statfilter, false)
	}
	getData();
}

function getData() {
	lstTr = document.getElementsByTagName('TR');
	for (d = 1; d < lstTr.length; d++) {
		membre = new Object();
		for (c = 0; c < lstTr[d].children.length; c++) {
			// data.lstTr[d].children[c].textContent.lstTr[0].children[d].id =
			// lstTr[d].children[c].textContent;
			membre[lstTr[0].children[c].id] = lstTr[d].children[c].textContent;
		}
		donneesMembre[d - 1] = membre;
	}
	donneesMembre.length = d - 1;
}

function filtrerTableau(donnee) {
	var thFilter = document.getElementById(donnee);
	var imgFilter = document.getElementById('filter');
	thFilter.appendChild(imgFilter);
	donneesTriees = new Object();
	if (sortData == donnee) {
		if (sorting == 'asc') {
			sorting = 'desc';
		} else {
			sorting = 'asc';
		}
	}
	else {
		sortData = donnee
		sorting = 'asc'
	}
	imgFilter.src = wotHub+'/images/'+sorting+'.png';
	lstTr = document.getElementsByTagName('TR');
	clanStat = lstTr[0].parentElement;
	var tri = new Array();
	for (d = lstTr.length - 1; d > 0; d--) {
		for (e = lstTr[d].children.length - 1; e >= 0; e--) {
			lstTr[d].removeChild(lstTr[d].children[e]);
		}
		clanStat.removeChild(lstTr[d]);
	}
	for (m = 0; m < donneesMembre.length; m++) {
		if (donnee == 'f_nom') {
			var indice = m;
		}
		else {
			var indice = cleanStat(donneesMembre[m][donnee],m)
		}
		donneesTriees[indice] = donneesMembre[m];
		tri.push(indice);
	}
	if (sorting == 'asc') {
		tri.sort(function(a, b){return a-b});
	}
	else {
		tri.sort(function(a, b){return b-a});
	}
	pair = 1;
	for (index in tri) {
		var tr = genererLigne(donneesTriees[tri[index]]);
		if (pair % 2 == 0) {
			tr.className = "paire";
		}
		pair++;
		clanStat.appendChild(tr);
	}
}

function cleanStat(stat, indice) {
	if (indice <= 9) {
		indice = '0'+indice;
	}
	maStat = stat.replace(',', '');
	maStat = maStat.replace(' %', '');
	maStat = maStat.replace(' ', '');
	maStat = maStat + indice;
//	for (l = 0; l <= 10-maStat.length; l++) {
//		maStat = '0'+maStat;
//	}
	return Number(maStat);
}

function genererLigne(objet) {
	var tr = document.createElement('tr');
	for (i = 0; i < tdOrder.length; i++) {
		var td = document.createElement('td');
		if (tdOrder[i] === 'f_er' || tdOrder[i] === 'f_wn7'
				|| tdOrder[i] === 'f_wn8') {
			td.className = setRatingRang(tdOrder[i], objet[tdOrder[i]]);
		}
		td.textContent = objet[tdOrder[i]];
		tr.appendChild(td);
	}
	return tr;
}

function setRatingRang(rating, valeur) {
	rat = rating.substr(2).toUpperCase();
	for (s = 0; s < statsRangs[rat].length; s++) {
		if (valeur < statsRangs[rat][s] ) {
			return statsRangs[rat+'_Rg'][s];
		}
	}
}